import 'package:flutter/material.dart';

class AQInput extends StatelessWidget {
  final bool readOnly;
  final String? label;
  final String? placeholder;
  final IconData? prefixIcon;
  final IconData? suffixIcon;
  final bool obscureText;
  final String? Function(String?)? validator;
  final void Function(String, TextEditingController)? onChanged;
  final Future<void> Function(TextEditingController)? onTap;
  final TextEditingController? controller;
  final String? defaultValue;

  const AQInput({
    super.key,
    this.label,
    this.placeholder,
    this.prefixIcon,
    this.suffixIcon,
    this.controller,
    this.obscureText = false,
    this.readOnly = false,
    this.validator,
    this.onChanged,
    this.onTap,
    this.defaultValue,
  });

  static const _borderColor = Color.fromRGBO(217, 217, 217, 1);
  static const _textColor = Color.fromRGBO(217, 217, 217, 1);
  static const _borderRadius = BorderRadius.all(Radius.circular(4));

  @override
  Widget build(BuildContext context) {
    final effectiveController = controller ?? TextEditingController(text: defaultValue);
    return TextFormField(
      readOnly: readOnly,
      controller: effectiveController,
      obscureText: obscureText,
      cursorColor: Colors.black,
      decoration: InputDecoration(
        labelText: label,
        hintText: placeholder,
        prefixIcon: prefixIcon != null ? Icon(prefixIcon) : null,
        suffixIcon: suffixIcon != null ? Icon(suffixIcon) : null,
        border: _border(),
        focusedBorder: _focusedBorder(),
        enabledBorder: _enabledBorder(),
        labelStyle: _textStyle(),
        hintStyle: _textStyle(),
        prefixIconColor: _borderColor,
        suffixIconColor: _borderColor,
      ),
      validator: validator,
      onChanged: (value) {
        if (onChanged != null) {
          onChanged!(value, effectiveController);
        }
      },
      onTap: () async {
        if (onTap != null) {
          await onTap!(effectiveController);
        }
      },
    );
  }

  OutlineInputBorder _border() {
    return const OutlineInputBorder(
      borderRadius: _borderRadius,
    );
  }

  OutlineInputBorder _focusedBorder() {
    return const OutlineInputBorder(
      borderRadius: _borderRadius,
      borderSide: BorderSide(color: _borderColor, width: 1.0),
    );
  }

  OutlineInputBorder _enabledBorder() {
    return const OutlineInputBorder(
      borderRadius: _borderRadius,
      borderSide: BorderSide(color: _borderColor, width: 1.0),
    );
  }

  TextStyle _textStyle() {
    return const TextStyle(color: _textColor);
  }
}
